<?php
namespace App\Http\Controllers\Business\ShopWeb\SalesmanManagement;

use App\Models\ShopSalesmanWithdrawal;
use App\Models\ShopUser;
use App\Models\ShopSalesman;
use App\Models\ShopSalesmanDetail;
use App\Models\Order;
use Illuminate\Support\Facades\Redis;

class SalesmanManagement {

    //地推注册的列表
    public static function registerList($shopIds, $shopUserId=0, $startTime='', $lastTime='', $status='%', $paginate=50) {
        if(empty($shopIds)) {
            return ['status'=>'failed', 'message'=>'参数错误'];
        }
        if(!$startTime||!$lastTime) {
            $startTime = '2016-07-01 00:00:00';
            $lastTime = date('Y-m-d') . " 23:59:59";
        }
        if($shopUserId == '-1') {
            //全部
            $data = ShopSalesmanDetail::whereIn('shop_salesman_detail.shopid', $shopIds)
                ->where('shop_salesman_detail.created_at', '>=', $startTime)->where('shop_salesman_detail.created_at', '<=', $lastTime)
                ->where('shop_salesman_detail.accountstatus', 'like', $status)
                ->where('shop_salesman_detail.type', 'register')
                ->where('shop_salesman_detail.getmoney', '<>', 0.00)
                ->join('shops', 'shop_salesman_detail.shopid', '=', 'shops.id')
                ->orderBy('shop_salesman_detail.created_at', 'desc')
                ->select('shop_salesman_detail.created_at', 'shop_salesman_detail.mobile', 'shops.name as shopname', 'shop_salesman_detail.shopuserid', 'shop_salesman_detail.shopusermobile')
                ->paginate($paginate);
        } elseif($shopUserId == 0) {
            //默认
            $data = ShopSalesmanDetail::whereIn('shop_salesman_detail.shopid', $shopIds)
                ->where('shop_salesman_detail.created_at', '>=', $startTime)->where('shop_salesman_detail.created_at', '<=', $lastTime)
                ->where('shop_salesman_detail.accountstatus', 'like', $status)
                ->where('shop_salesman_detail.type', 'register')
                ->where('shop_salesman_detail.getmoney', '<>', 0.00)
                ->where('shop_salesman_detail.shopuserid', 0)
                ->join('shops', 'shop_salesman_detail.shopid', '=', 'shops.id')
                ->orderBy('shop_salesman_detail.created_at', 'desc')
                ->select('shop_salesman_detail.created_at', 'shop_salesman_detail.mobile', 'shops.name as shopname', 'shop_salesman_detail.shopuserid', 'shop_salesman_detail.shopusermobile')
                ->paginate($paginate);
        } else {
            //选择shopuserid
            $data = ShopSalesmanDetail::whereIn('shop_salesman_detail.shopid', $shopIds)
                ->where('shop_salesman_detail.created_at', '>=', $startTime)->where('shop_salesman_detail.created_at', '<=', $lastTime)
                ->where('shop_salesman_detail.accountstatus', 'like', $status)
                ->where('shop_salesman_detail.type', 'register')
                ->where('shop_salesman_detail.getmoney', '<>', 0.00)
                ->where('shop_salesman_detail.shopuserid', $shopUserId)
                ->join('shops', 'shop_salesman_detail.shopid', '=', 'shops.id')
                ->orderBy('shop_salesman_detail.created_at', 'desc')
                ->select('shop_salesman_detail.created_at', 'shop_salesman_detail.mobile', 'shops.name as shopname', 'shop_salesman_detail.shopuserid', 'shop_salesman_detail.shopusermobile')
                ->paginate($paginate);
        }
        foreach($data as $k => $v) {
            if(($data[$k]['shopuserid'] == 0) || ($data[$k]['shopuserid'] == '')) {
                $data[$k]['shopusername'] = '默认';
            } else {
                $data[$k]['shopusername'] = ShopUser::where('id', $data[$k]['shopuserid'])->pluck('name')->first();
            }
            $data[$k]['mobile'] = str_replace(substr($data[$k]['mobile'], 3, 4), '****', $data[$k]['mobile']);
        }
        return ['status'=>'success', 'data'=>$data];
    }
    //地推订单列表
    public static function orderList($shopIds, $shopUserId=0, $startTime='', $lastTime='',$status='%',$paginate=50) {
        if(empty($shopIds)) {
            return ['status'=>'failed', 'message'=>'参数错误'];
        }
        if(!$startTime||!$lastTime) {
            $startTime = '2016-07-01 00:00:00';
            $lastTime = date('Y-m-d') . " 23:59:59";
        }
        if($shopUserId == -1) {
            //全部
            $lists = ShopSalesmanDetail::whereIn('shop_salesman_detail.shopid', $shopIds)
                ->where('shop_salesman_detail.created_at', '>=', $startTime)->where('shop_salesman_detail.created_at', '<=', $lastTime)
                ->where('shop_salesman_detail.accountstatus', 'like', $status)->where('shop_salesman_detail.getmoney', '>', 0)
                ->where('shop_salesman_detail.type', 'order')
                ->join('shops', 'shop_salesman_detail.shopid', '=', 'shops.id')
                ->orderBy('shop_salesman_detail.created_at', 'desc')
                ->select('shop_salesman_detail.created_at', 'shop_salesman_detail.mobile', 'shop_salesman_detail.orderamount', 'shops.name as shopname', 'shop_salesman_detail.shopuserid', 'shop_salesman_detail.shopusermobile')
                ->paginate($paginate);
            $count = ShopSalesmanDetail::whereIn('shopid', $shopIds)
                ->where('created_at', '>=', $startTime)->where('created_at', '<=', $lastTime)
                ->where('accountstatus', 'like', $status)->where('getmoney', '>', 0)
                ->where('type', 'order')->count();
            $sum = ShopSalesmanDetail::whereIn('shopid', $shopIds)
                ->where('created_at', '>=', $startTime)->where('created_at', '<=', $lastTime)
                ->where('accountstatus', 'like', $status)->where('getmoney', '>', 0)
                ->where('type', 'order')
                ->sum('orderamount');
        } elseif($shopUserId == 0) {
            //默认
            $lists = ShopSalesmanDetail::whereIn('shop_salesman_detail.shopid', $shopIds)
                ->where('shop_salesman_detail.created_at', '>=', $startTime)->where('shop_salesman_detail.created_at', '<=', $lastTime)
                ->where('shop_salesman_detail.accountstatus', 'like', $status)->where('shop_salesman_detail.getmoney', '>', 0)
                ->where('shop_salesman_detail.type', 'order')
                ->where('shop_salesman_detail.shopuserid', 0)
                ->join('shops', 'shop_salesman_detail.shopid', '=', 'shops.id')
                ->orderBy('shop_salesman_detail.created_at', 'desc')
                ->select('shop_salesman_detail.created_at', 'shop_salesman_detail.mobile', 'shop_salesman_detail.orderamount', 'shops.name as shopname', 'shop_salesman_detail.shopuserid', 'shop_salesman_detail.shopusermobile')
                ->paginate($paginate);
            $count = ShopSalesmanDetail::whereIn('shopid', $shopIds)
                ->where('created_at', '>=', $startTime)->where('created_at', '<=', $lastTime)
                ->where('accountstatus', 'like', $status)->where('getmoney', '>', 0)
                ->where('type', 'order')->where('shopuserid', 0)
                ->count();
            $sum = ShopSalesmanDetail::whereIn('shopid', $shopIds)
                ->where('created_at', '>=', $startTime)->where('created_at', '<=', $lastTime)
                ->where('accountstatus', 'like', $status)->where('getmoney', '>', 0)
                ->where('type', 'order')->where('shopuserid', 0)
                ->sum('orderamount');
        } else {
            $lists = ShopSalesmanDetail::whereIn('shop_salesman_detail.shopid', $shopIds)
                ->where('shop_salesman_detail.created_at', '>=', $startTime)->where('shop_salesman_detail.created_at', '<=', $lastTime)
                ->where('shop_salesman_detail.accountstatus', 'like', $status)->where('shop_salesman_detail.getmoney', '>', 0)
                ->where('shop_salesman_detail.type', 'order')
                ->where('shop_salesman_detail.shopuserid', $shopUserId)
                ->join('shops', 'shop_salesman_detail.shopid', '=', 'shops.id')
                ->orderBy('shop_salesman_detail.created_at', 'desc')
                ->select('shop_salesman_detail.created_at', 'shop_salesman_detail.mobile', 'shop_salesman_detail.orderamount','shops.name as shopname', 'shop_salesman_detail.shopuserid', 'shop_salesman_detail.shopusermobile')
                ->paginate($paginate);
            $count = ShopSalesmanDetail::whereIn('shopid', $shopIds)
                ->where('created_at', '>=', $startTime)->where('created_at', '<=', $lastTime)
                ->where('accountstatus', 'like', $status)->where('getmoney', '>', 0)
                ->where('type', 'order')
                ->where('shopuserid', $shopUserId)
                ->sum('orderamount');
            $sum = ShopSalesmanDetail::whereIn('shopid', $shopIds)
                ->where('created_at', '>=', $startTime)->where('created_at', '<=', $lastTime)
                ->where('accountstatus', 'like', $status)->where('getmoney', '>', 0)
                ->where('type', 'order')
                ->where('shopuserid', $shopUserId)
                ->sum('orderamount');
        }
        foreach($lists as $k => $v) {
            if(($lists[$k]['shopuserid'] == 0) || ($lists[$k]['shopuserid'] == '')) {
                $lists[$k]['shopusername'] = '默认';
            } else {
                $lists[$k]['shopusername'] = ShopUser::where('id', $lists[$k]['shopuserid'])->pluck('name')->first();
            }
            $lists[$k]['mobile'] = str_replace(substr($lists[$k]['mobile'], 3, 4), '****', $lists[$k]['mobile']);
        }
        if(!$sum) {
            $sum = 0;
        }
        $data = [
            'lists' => $lists,
            'count' => $count,
            'sum' => $sum
        ];
        return ['status'=>'success', 'data'=>$data];
    }
}